package ar.uba.fi.tonyvaliente.text.filtering;

import java.util.LinkedList;

public class TermsFilter implements TextFilter {
	public LinkedList<String> filter(LinkedList<String> text) {
		LinkedList<String> result = new LinkedList<String>();

		for (String s : text) {
			result.addAll(split(s));
		}
		return result;
	}

	private LinkedList<String> split(String text) {

		LinkedList<String> result = new LinkedList<String>();
		int begin = -1, end = 0;
		for (int i = 0; i < text.length(); i++) {
			char c = text.charAt(i);
			if (Character.isLetter(c)) {
				if (begin == -1)
					begin = end = i;
				end++;
			} else {
				if (begin != -1 && end != -1) {
					CharSequence seq = text.subSequence(begin, end);
					result.add(seq.toString());

					begin = -1;
					end = -1;
				}
			}
		}
		if (begin != -1 && end != -1) {
			CharSequence seq = text.subSequence(begin, end);
			result.add(seq.toString());
		}
		return result;
	}
}
